package com.news.news.service;


import com.news.news.dao.UserLoginDao;
import com.news.news.modle.News;
import com.news.news.modle.User;
import com.news.news.util.CommonResult;
import com.news.news.util.JWTUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service
@Transactional
public class UsersLoginService {
    @Resource
    UserLoginDao userLoginDao;


    public User login(User user) {
        User login = userLoginDao.login(user);
        if(login!=null){
            String adminToken = JWTUtil.getUserToken(login);
            login.setUserToken(adminToken);
        }
        return login;
    }

    public void saveUserNews(News news, String token) {
        news.setAdminid(JWTUtil.getTokenInfo(token).getClaim("id").asInt());
        //保存信息并返回ID
        userLoginDao.saveNews(news);
        //添加新闻与新闻类型关系
        if(news.getNewsTypeid()!=null){
            for (Integer NewsTypeid : news.getNewsTypeid()) {
                userLoginDao.addNewstypeRole(news.getId(),NewsTypeid);
            }
        }
        //绑定用户与新闻的关系
        userLoginDao.saveUserNewsRole(news);
    }
}
